<?php

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ShopController extends Controller
{
	//
	public function getData()
	{
		$shops = Product::orderBy('id', 'asc')->get();
		return $shops;
	}
	
	public function info()
	{
		$id = $_GET['id'];
		$res = DB::table('product')->where(['id' => $id])->get();
		return $res;
	}
	
	public function create(Request $request)
	{
		$id = $request->id;
		$price = $request->price;
		$num = $request->num;
		$rows = DB::table('buycar')->where(['product_id' => $id])->get();
		$data = json_decode($rows, true);
		if (!$data == '') {
			return json_encode(['code' => 3, 'message' => '已添加，无需再次添加']);
		} else {
			$res = Buycar::create([
				'user_id' => 1,
				'product_id' => $id,
				'num' => $num,
				'unit_price' => $price,
			]);
			if ($res) {
				return json_encode(['code' => 1, 'message' => '添加成功']);
			} else {
				return json_encode(['code' => 0, 'message' => '添加失败']);
			}
		}
	}
}
